package com.beiing.tianshuai.tianshuai.util;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;

import java.text.MessageFormat;

/**
 * Created by Jinni on 2017/12/13 0013
 */

public class TimeCount extends CountDownTimer {

    private Context mContext;
    private View mView;
    private boolean isFinish;

    public boolean isFinish() {
        return isFinish;
    }

    public TimeCount(long millisInFuture, long countDownInterval, View view, Context context) {
        super(millisInFuture, countDownInterval);
        mContext = context;
        mView = view;
    }

    @Override
    public void onFinish() {
        if (mView instanceof TextView) {
            isFinish = true;
            ((TextView) mView).setText("重新获取");
            ((TextView) mView).setTextColor(ContextCompat.getColor(mContext, R.color.text_login_main_color));
            mView.setClickable(true);
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (mView instanceof TextView) {
            isFinish = false;
            mView.setClickable(false);
            long l = millisUntilFinished / 1000;
            ((TextView) mView).setText(MessageFormat.format("{0}秒", String.valueOf(l)));
            ((TextView) mView).setTextColor(ContextCompat.getColor(mContext, R.color.text_gray_light2));
        }
    }
}
